package com.zj.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zj.common.Result;
import com.zj.common.Utility;
import com.zj.entity.Goods;
import com.zj.entity.Orders;
import com.zj.entity.UserInfo;
import com.zj.mapper.OrderMapper;
import com.zj.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 创建订单  类型：购买
     *
     * @param userInfo 用户信息
     * @param goods    货物
     * @return {@code Result}
     */
    public Result createOrderByBuy(UserInfo userInfo, Goods goods){
        Orders orders = new Orders();
        orders.setBuyUserId(userInfo.getUserId()); //买家
        orders.setSellUserId(goods.getUserId());   //卖家
        orders.setUuid(Utility.getUUID());          //订单uid
        orders.setGoodsId(goods.getGoodsId());    //交易商品id
        orders.setType(1);                        //订单类型是 购买
        orders.setStatus(1);                      //订单状态  创建
        orders.setAddress(goods.getAddress()); // 交易地址 我的地址
        orders.setCreatTime(Utility.getCurrentDate());  //订单创建时间
        orders.setGoodsUuid(goods.getUuid());    //商品uid
        orders.setContactInformation(goods.getContactInformation()); //卖家的联系信息
        orderMapper.insert(orders);
        return Result.success("下单成功，请主动联系卖家！");
    }

    /**
     * 创建订单 类型：交换
     *
     * @param userInfo  用户信息
     * @param getGoods  得到货物
     * @param pushGoods 推动商品
     * @return {@code Result}
     */
    public Result createOrderByExchange(UserInfo userInfo, Goods getGoods,Goods pushGoods){
        Orders orders = new Orders();
        orders.setBuyUserId(userInfo.getUserId()); //买家
        orders.setSellUserId(getGoods.getUserId());   //卖家
        orders.setUuid(getGoods.getUuid());
        orders.setGoodsId(getGoods.getGoodsId());
        orders.setType(2);
        orders.setStatus(1);
        orders.setAddress(getGoods.getAddress());
        orders.setCreatTime(Utility.getCurrentDate());
        orders.setGoodsUuid(Utility.getUUID());
        orders.setContactInformation(getGoods.getContactInformation());
        orders.setGoodsIdExchange(pushGoods.getGoodsId());  //用来交换的 商品id
        orderMapper.insert(orders);
        return Result.success("下单成功，请主动联系卖家！");
    }
    @Override
    public Result finishOrder(UserInfo userInfo, Goods goods) {
        return null;
    }

    @Override
    public Result cancelOrder(UserInfo userInfo, Goods goods) {
        return null;
    }
}
